package tarea1graficas;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.*;
import javafx.util.Math;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.control.Button;
import tarea1graficas.CloseButtom.CloseButton;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

class Point3D {
   var x: Double;
   var y: Double;
   var z: Double;
   var screenX: Number;
   var screenY: Number;
}

class Edge {
   var a: Integer;
   var b: Integer;
}
var width: Number = 1024 ;
var height: Number = 768;
var azimuth: Number = 30;
var elevation: Number = 30;
var vertices: Point3D[] = [];

function addVertex(x: Double, y: Double, z: Double) {
   insert
      Point3D {
         x: x,
         y: y,
         z: z
      }
   into vertices;
}
var angle:Number =1;
for(i in [1..60]){
        addVertex(0, 0,1 );
        addVertex(0.5*Math.cos(angle*i),0.5*Math.sin(angle*i), -0.5);
}
var edges: Edge[] = [];
function addEdge(a: Integer, b: Integer) {
   insert
      Edge { a: a,
         b: b
      }
   into edges;
}
for(i in [0..60]){
addEdge(0,9);
addEdge(i, 0);
}
var points: Circle[] = [];
var lines: Polygon[] = [];

function movePoints(createFirstTime: Boolean): Void {
   var theta: Number = bind 0.017453 * azimuth;
   var phi = 0.017453 * elevation;
   var cosTheta = Math.cos(theta);
   var sinTheta = Math.sin(theta);
   var cosPhi = Math.cos(phi);
   var sinPhi = Math.sin(phi);
   var cosThetaCosPhi = cosTheta * cosPhi;
   var cosThetaSinPhi = cosTheta * sinPhi;
   var sinThetaCosPhi = sinTheta * cosPhi;
   var sinThetaSinPhi = sinTheta * sinPhi;
   var scaleFactor =  width / 5;
   for (i in [0..sizeof vertices - 1]) {
      var x0 = vertices[i].x;
      var y0 = vertices[i].y;
      var z0 = vertices[i].z;
      var x1 = cosTheta * x0 + sinTheta * z0;
      var y1 = -sinThetaSinPhi * x0 + cosPhi * y0 + cosThetaSinPhi * z0;
      var z1 = cosThetaCosPhi * z0 - sinThetaCosPhi * x0 - sinPhi * y0;
      x1 = x1 * 3 / (z1 + 4.5);
      y1 = y1 * 3 / (z1 + 4.5);
      vertices[i].screenX = x1;
      vertices[i].screenY = y1;
      if (createFirstTime) {
         insert
            Circle {
               centerX: bind 500 / 2 + scaleFactor * vertices[i].screenX + 0.5,
               centerY: bind 400 / 2 - scaleFactor * vertices[i].screenY + 0.5,
               radius: 2
               fill: Color.RED
            }
         into points;
      }
   }

   if (createFirstTime) {
      for (edge in edges) {
         insert
          Polygon {
         points:bind [
             points[edge.a].centerX
            ,  points[edge.a].centerY
            , points[edge.b].centerX
            , points[edge.b].centerY
            ]
            fill:Color.WHITE;
            strokeWidth: 7,
            stroke: LinearGradient {
            startX: 0.0 startY: 0.0
            endX: 0.0  endY : 1.0
            stops: [
                Stop {
                    color : Color.RED
                    offset: 0.0
                },
                Stop {
                    color : Color.BLUE
                    offset: 0.25
                },
                Stop {
                    color : Color.YELLOWGREEN
                    offset: 1.0
                }
                ]
                }

         }
         into lines;
      }
   }
}
movePoints(true);


var mouseX=130;
var mouseY=160;
var galaxy: Rectangle = Rectangle {
   x: 0,
   y: 0
   width: 280,
   height: 280
   translateX:110;
   translateY:60;
   fill: Color.BLACK


   onMouseDragged: function (me: MouseEvent): Void {
      var new_mx = me.x;
      azimuth -= new_mx - mouseX;
 

      var new_my = me.y;
      elevation += new_my - mouseY;
      mouseX = new_mx;
      mouseY = new_my;
      movePoints(false);
   }

} // galaxy
var verticespris: Point3D[] = [];

function addVertexpris(x: Double, y: Double, z: Double) {
   insert
      Point3D {

         x: x,
         y: y,
         z: z
      }
   into verticespris;
}

        addVertexpris(0,-1,-1); //pto 0,0
        addVertexpris(1,-1,-1);
        addVertexpris(0,1,-1);
        addVertexpris(0,-1,1);
        addVertexpris(1,1,-1);
        addVertexpris(0,1,1);
        addVertexpris(-1,-1,-1);//(6)
        addVertexpris(-1,1,-1);//(7)



var edgespris: Edge[] = [];
function addEdgepris(a: Integer, b: Integer) {
   insert
      Edge { a: a,
         b: b
      }
   into edgespris;
}
addEdgepris(0,0);
addEdgepris(0,1);
addEdgepris(0,2);
addEdgepris(2,0);
addEdgepris(0,3);
addEdgepris(1,3);
addEdgepris(2,4);
addEdgepris(4,1);
addEdgepris(5,4);
addEdgepris(5,2);
addEdgepris(5,3);
addEdgepris(6,7);
addEdgepris(6,3);
addEdgepris(6,0);
addEdgepris(7,5);
addEdgepris(7,4);




var pointspris: Circle[] = [];
var linespris: Polygon[] = [];


function movePointspris(createFirstTime: Boolean): Void {
   var theta: Number = bind 0.017453 * azimuth;
   var phi = 0.017453 * elevation;
   var cosTheta = Math.cos(theta);
   var sinTheta = Math.sin(theta);
   var cosPhi = Math.cos(phi);
   var sinPhi = Math.sin(phi);
   var cosThetaCosPhi = cosTheta * cosPhi;
   var cosThetaSinPhi = cosTheta * sinPhi;
   var sinThetaCosPhi = sinTheta * cosPhi;
   var sinThetaSinPhi = sinTheta * sinPhi;
   var scaleFactor =  width / 8;
   for (i in [0..sizeof verticespris - 1]) {
      var x0 = verticespris[i].x;
      var y0 = verticespris[i].y;
      var z0 = verticespris[i].z;
      var x1 = cosTheta * x0 + sinTheta * z0;
      var y1 = -sinThetaSinPhi * x0 + cosPhi * y0 + cosThetaSinPhi * z0;
      var z1 = cosThetaCosPhi * z0 - sinThetaCosPhi * x0 - sinPhi * y0;
      x1 = x1 * 3 / (z1 + 4.5);
      y1 = y1 * 3 / (z1 + 4.5);
      verticespris[i].screenX = x1;
      verticespris[i].screenY = y1;

      if (createFirstTime) {
         insert
            Circle {
               centerX: bind 1475 / 2 + scaleFactor * verticespris[i].screenX + 0.5,
               centerY: bind 400 / 2 - scaleFactor * verticespris[i].screenY + 0.5,
               radius: 2
               fill: Color.RED

           }
         into pointspris;
      }
   }

   if (createFirstTime) {
      for (edge in edgespris) {
         insert
         Polygon {
         points:bind [
             pointspris[edge.a].centerX
            ,  pointspris[edge.a].centerY
            , pointspris[edge.b].centerX
            , pointspris[edge.b].centerY
            ]
            strokeWidth: 2,
            stroke: Color.RED
         }
         into linespris;
      }
   }
}
movePointspris(true);
var mouseXpris: Number;
var mouseYpris: Number;
var galaxy2: Rectangle = Rectangle {
   x: 0,
   y: 0
   width: 280,
   height: 280
   translateX:600;
   translateY:60;
   fill: Color.BLACK

   onMousePressed: function (me: MouseEvent): Void {
      mouseXpris = me.x;
      mouseYpris = me.y;
   }

   onMouseDragged: function (me: MouseEvent): Void {
      var new_mx = me.x;
      azimuth -= new_mx - mouseXpris;
      var new_my = me.y;
      elevation += new_my - mouseYpris;
      mouseXpris = new_mx;
      mouseYpris = new_my;
      movePointspris(false);
   }

}



var imgXs;
var imgYs;
var cont:Number;
var contador=Label {
        layoutX:10
        layoutY:300
	text: bind "{cont as Number}"
        font: Font {
        name: "Bitstream Vera Sans Bold"
        size: 28
    }
}

 var images = [
            Image { url: "{__DIR__}cajate.png" },
            Image { url: "{__DIR__}dado.png" },
            Image { url: "{__DIR__}discos.png" }
            Image { url: "{__DIR__}gorrocumple.png" },
            Image { url: "{__DIR__}lata.png" },
            Image { url: "{__DIR__}late.png" }
            Image { url: "{__DIR__}pelota.png" },
            Image { url: "{__DIR__}tarropapa.png" },
            Image { url: "{__DIR__}torrecd.png" },
            Image { url: "{__DIR__}vela.png" }
            Image { url: "{__DIR__}cone.png" },
            Image { url: "{__DIR__}prismauno.png" },

        ]
        ;


var count: Integer = 1;
var cajate=ImageView{
var imgX : Number = 850;
var imgY : Number = 150;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct:Boolean;
var cta:Boolean;
                       x:bind imgX;
                       y:bind imgY;
                        fitWidth:150
                        fitHeight:100
                        image : images[0]
                           onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;

                        startY = e.y;

                        distX = startX - imgX;

                        distY = startY - imgY;
                        }
                        onMouseDragged: function( e: MouseEvent ):Void {
                        imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   ct=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   cta=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=2000;
                                   imgY=2000;
                                   cont=cont+1;
                                   ct=false

                            }if(cta){
                                imgX=850;
                                imgY=150;
                                 cont=cont-1;
                                 cta=false
                                 }

    }                    }

var dado=ImageView {
var imgX : Number = 5;
var imgY : Number = 10;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct:Boolean
var cta:Boolean
                        x:bind imgX;
                        y:bind imgY;
                        fitWidth:150
                        fitHeight:87
                        image : images[1]
                        onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;
                        startY = e.y;
                        distX = startX - imgX;
                        distY = startY - imgY;
                    }
                    onMouseClicked: function(e: MouseEvent): Void {
                    startX=e.x;
                    startY=e.y
}
                    onMouseDragged: function( e: MouseEvent ):Void {
                        imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   ct=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   cta=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=5;
                                   imgY=10;
                                   cont=cont-1;
                                   ct=false
                            }if(cta){
                                imgX=5;
                                imgY=10;
                                 cont=cont-1;
                                cta=false
                                }


    }                    }
var discos=ImageView{
var imgX : Number = 150;
var imgY : Number = 10;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct;
var cta;
                       x:bind imgX;
                       y:bind imgY;
                        fitWidth:150
                        fitHeight:87
                        image : images[2]
                           onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;

                        startY = e.y;

                        distX = startX - imgX;

                        distY = startY - imgY;
                    }
                    onMouseDragged: function( e: MouseEvent ):Void {
                        imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   ct=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   cta=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=150;
                                   imgY=10;
                                   cont=cont-1;
                                   ct=false
                            }if(cta){
                                imgX=150;
                                imgY=10;
                                 cont=cont-1;
                                cta=false
                                }


    }                    }
var gorrocumple=ImageView {
var imgX : Number = 200;
var imgY : Number = 100;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct:Boolean
var cta:Boolean
                       x:bind imgX;
                       y:bind imgY;
                       fitWidth:120
                       fitHeight:87
                       image :bind images[3]
                       onMousePressed: function( e: MouseEvent ):Void {
                       startX = e.x;
                       startY = e.y;
                       distX = startX - imgX;
                       distY = startY - imgY;
                    }

                       onMouseClicked: function(e: MouseEvent): Void {
                       startX=e.x;
                       startY=e.y
}                      onMouseDragged: function( e: MouseEvent ):Void {
                        imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   cta=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   ct=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=2000;
                                   imgY=2000;
                                   cont=cont+1;
                                   ct=false
                            }if(cta){
                                imgX=157;
                                imgY=87;
                                 cont=cont-1;
                                cta=false
                                }


    }                    }

var lata=ImageView {
var imgX : Number = 250;
var imgY : Number = 10;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct;
var cta;
                       x:bind imgX;
                       y:bind imgY;

                        fitWidth:150
                        fitHeight:87
                        image : images[4]
                              onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;

                        startY = e.y;

                        distX = startX - imgX;

                        distY = startY - imgY;
                    }
                    onMouseDragged: function( e: MouseEvent ):Void {
                        imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   ct=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   cta=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=250;
                                   imgY=10;
                                   cont=cont-1;
                                   ct=false
                            }if(cta){
                                imgX=250;
                                imgY=10;
                                 cont=cont-1;
                                cta=false
                                }


    }                    }
var late=ImageView {
var imgX : Number = 400;
var imgY : Number = 10;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct:Boolean
var cta:Boolean
                       x:bind imgX;
                       y:bind imgY;

                        fitWidth:150
                        fitHeight:87
                        image : images[5]
                              onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;

                        startY = e.y;

                        distX = startX - imgX;

                        distY = startY - imgY;
                    }
                    onMouseDragged: function( e: MouseEvent ):Void {
                             imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   ct=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   cta=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=2000;
                                   imgY=2000;
                                   cont=cont+1;
                                   ct=false
                            }if(cta){
                                imgX=400;
                                imgY=10;
                                 cont=cont-1;
                                cta=false
                                }


    }                    }
var pelota=ImageView {
var imgX : Number = 550;
var imgY : Number = 10;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct;
var cta;
                       x:bind imgX;
                       y:bind imgY;

                        fitWidth:150
                        fitHeight:87
                        image : images[6]
                              onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;

                        startY = e.y;

                        distX = startX - imgX;

                        distY = startY - imgY;
                    }
                    onMouseDragged: function( e: MouseEvent ):Void {
                                               imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   ct=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   cta=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=550;
                                   imgY=10;
                                   cont=cont-1;
                                   ct=false
                            }if(cta){
                                imgX=550;
                                imgY=10;
                                 cont=cont-1;
                                cta=false
                                }


    }                    }
var tarropapa=ImageView {
var imgX : Number = 700;
var imgY : Number = 10;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct;
var cta;
                       x:bind imgX;
                       y:bind imgY;

                        fitWidth:150
                        fitHeight:87
                        image : images[7]
                              onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;

                        startY = e.y;

                        distX = startX - imgX;

                        distY = startY - imgY;
                    }
                    onMouseDragged: function( e: MouseEvent ):Void {
                                               imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   ct=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   cta=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=700;
                                   imgY=10;
                                   cont=cont-1;
                                   ct=false
                            }if(cta){
                                imgX=700;
                                imgY=10;
                                 cont=cont-1;
                                cta=false
                                }


    }                    }
var torrecd=ImageView {
var imgX : Number = 800;
var imgY : Number = 10;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct:Boolean;
var cta:Boolean;
                        x:bind imgX;
                       y:bind imgY;

                        fitWidth:150
                        fitHeight:87
                        image : images[8]
                              onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;

                        startY = e.y;

                        distX = startX - imgX;

                        distY = startY - imgY;
                    }
                    onMouseDragged: function( e: MouseEvent ):Void {
                             imgX = e.x - distX;
                        imgY = e.y - distY;
                        if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   ct=true        }
                        }
                        if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   cta=true  ;
                                   }
                     }
}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=2000;
                                   imgY=2000;
                                   cont=cont+1;
                                   ct=false
                            }if(cta){
                                imgX=800;
                                imgY=10;
                                 cont=cont-1;
                                cta=false
                                }


    }                    }

var vela=ImageView {
var imgX : Number = 900;
var imgY : Number = 10;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
var ct:Boolean;
var cta:Boolean
                       x: bind imgX;
                       y: bind imgY;
                        fitWidth:150
                        fitHeight:87
                        image : images[9]
                              onMousePressed: function( e: MouseEvent ):Void {
                        startX = e.x;

                        startY = e.y;

                        distX = startX - imgX;

                        distY = startY - imgY;
                    }
                    onMouseDragged: function( e: MouseEvent ):Void {
                             imgX = e.x - distX;
                        imgY = e.y - distY;

                           if(e.x>=300 and e.x<=450){
                            if(e.y>=600 and e.y<=750){
                                   ct=true  ;
                                   }
                     }
                       if(e.x>=600 and e.x<=800){
                            if(e.y>=565 and e.y<=765){

                                   cta=true        }
                        }

}
                        onMouseReleased: function(e: MouseEvent): Void {
                        if (ct){
                                   imgX=2000;
                                   imgY=2000;
                                   cont=cont+1;
                                   ct=false
                            }if(cta){
                                imgX=900;
                                imgY=10;
                                 cont=cont-1;
                                 cta=false
                                }


    }                    }

var cono=ImageView {
                        y:600
                        x:300
                        fitWidth:203
                        fitHeight:186

                        image : images[10]

}
var prisma=ImageView {
                        y:565
                        x:600
                        fitWidth:203
                        fitHeight:186

                        image : images[11]

}




var grupo=Group {
	content: [
		dado,vela,torrecd,tarropapa,pelota,late,lata,gorrocumple,discos,cajate,
	]
}

def scen1: Scene = Scene {
        content:bind [
Text {
	font: Font {
		size: 14
	}
	x: 10, y: 300
	content: "Amigo!\nArrastra los objetos que se"
        "\nencuentran en la parte superior de la ventana y"
       "\ny llevalos hasta las figuras de "
       "\nmas abajo. Identifica que"
       "\nfigura corresponde a cada "
       "\nobjeto."
       "\nPor cada acierto, ganaras un"
       "\npunto! y si te equivocas,"
       "\nperderas uno."
}

            ImageView {
                x: 300.0, y: 200.0
                fitWidth:500
                fitHeight:320
                image: Image {
		url: "{__DIR__}baultaparriba.png"
            }
        }
        Button {
            translateX:900;
            translateY:600;
            text: "Button"
            action: function() {

	}
        }

        prisma,cono,
        contador,grupo            ]
        }
def scen: Scene = Scene {
var imagen1 = Image {
		url: "{__DIR__}baultaparriba.jpg"
	}
var imagen2 = Image {
		url: "{__DIR__}figura_azul.jpg"
	}
var Imagen_actual: Image = imagen1;

var imaa=Image { url: "{__DIR__}flecha-derecha.png" }

    content:bind[
        ImageView {
                image: bind Imagen_actual
                x: 300.0, y: 200.0
        }Text {
	font: Font {
		size: 18
	}
	x: 10, y: 30
	content: "Haz click en el Baul!"
}

       OpenBaul {
            onClicked: function():Void {
            currentScene=scen1;
          }
        }
        ]
}
var currentScene: Scene;
def scene: Scene = Scene {
   content: bind [

    CloseButton {
            onClicked: function():Void {
                currentScene=scen;
              }
        }

      galaxy,
      galaxy2,
      points,
      lines,
      linespris,
      pointspris,
      Text {
	font: Font {
		size: 14
	}
	x: 500, y: 500
	content: "Pincha y mueve las figuras con el mouse "
                    "\ny familiarizate con ellos."
                    "\nuna vez que las hayas visto preciona la flecha verde"
}


 ]
};
var stage=Stage {
   title: "Space Time"
   width: bind width
   height:bind  height
   scene:bind currentScene;

}
currentScene = scene;